#include "SimpleMeshLoader.h"
#include "OBB.h"
SimpleMeshLoader::SimpleMeshLoader( std::string path, IDirect3DDevice9 *device )
	:MeshLoader( path ), device(device)
{
	//near square
	positions.push_back( D3DXVECTOR3( -1, -1, -1) ); // left bot near
	positions.push_back( D3DXVECTOR3( -1, 1, -1 ) ); // left top near
	positions.push_back( D3DXVECTOR3( 1, 1, -1) ); // right top near
	positions.push_back( D3DXVECTOR3( 1, -1, -1 ) ); // right bot near
	//far square				  
	positions.push_back( D3DXVECTOR3( -1, -1, 1 ) ); // left bot far
	positions.push_back( D3DXVECTOR3( -1, 1, 1 ) ); // left top far
	positions.push_back( D3DXVECTOR3( 1, 1, 1) ); // right top far
	positions.push_back( D3DXVECTOR3( 1, -1, 1 ) ); // right bot far
	//left square				  
	positions.push_back( D3DXVECTOR3( -1, -1, -1 ) ); // left bot near
	positions.push_back( D3DXVECTOR3( -1, -1, 1 ) ); // left bot far
	positions.push_back( D3DXVECTOR3( -1, 1, 1 ) ); // left top far
	positions.push_back( D3DXVECTOR3( -1, 1, -1 ) ); // left top near
	//right square				   
	positions.push_back( D3DXVECTOR3( 1, -1, -1 ) ); // right bot near
	positions.push_back( D3DXVECTOR3( 1, -1, 1 ) ); // right bot far
	positions.push_back( D3DXVECTOR3( 1, 1, 1 ) ); // right top far
	positions.push_back( D3DXVECTOR3( 1, 1, -1 ) ); // right top near
	//top square				   
	positions.push_back( D3DXVECTOR3( -1, 1, -1 ) );// left top near
	positions.push_back( D3DXVECTOR3( -1, 1, 1 ) );// left top far
	positions.push_back( D3DXVECTOR3( 1, 1, 1 ) );// right top far
	positions.push_back( D3DXVECTOR3( 1, 1, -1 ) );// right top near
	//bottom square				   
	positions.push_back( D3DXVECTOR3( -1, -1, -1 ) );// left bot near
	positions.push_back( D3DXVECTOR3( -1, -1, 1 ) );// left bot far
	positions.push_back( D3DXVECTOR3( 1, -1, 1 ) );// right bot far
	positions.push_back( D3DXVECTOR3( 1, -1, -1 ) );// right bot near
	
	//near
	normals.push_back( D3DXVECTOR3( 0, 0, -1 ) );
	normals.push_back( D3DXVECTOR3( 0, 0, -1 ) );
	normals.push_back( D3DXVECTOR3( 0, 0, -1 ) );
	normals.push_back( D3DXVECTOR3( 0, 0, -1 ) );
	//far
	normals.push_back( D3DXVECTOR3( 0, 0, 1 ) );
	normals.push_back( D3DXVECTOR3( 0, 0, 1 ) );
	normals.push_back( D3DXVECTOR3( 0, 0, 1 ) );
	normals.push_back( D3DXVECTOR3( 0, 0, 1 ) );
	//left
	normals.push_back( D3DXVECTOR3( -1, 0, 0 ) );
	normals.push_back( D3DXVECTOR3( -1, 0, 0 ) );
	normals.push_back( D3DXVECTOR3( -1, 0, 0 ) );
	normals.push_back( D3DXVECTOR3( -1, 0, 0 ) );
	//right
	normals.push_back( D3DXVECTOR3( 1, 0, 0 ) );
	normals.push_back( D3DXVECTOR3( 1, 0, 0 ) );
	normals.push_back( D3DXVECTOR3( 1, 0, 0 ) );
	normals.push_back( D3DXVECTOR3( 1, 0, 0 ) );
	//top
	normals.push_back( D3DXVECTOR3( 0, 1, 0 ) );
	normals.push_back( D3DXVECTOR3( 0, 1, 0 ) );
	normals.push_back( D3DXVECTOR3( 0, 1, 0 ) );
	normals.push_back( D3DXVECTOR3( 0, 1, 0 ) );
	//bottom
	normals.push_back( D3DXVECTOR3( 0, -1, 0 ) );
	normals.push_back( D3DXVECTOR3( 0, -1, 0 ) );
	normals.push_back( D3DXVECTOR3( 0, -1, 0 ) );
	normals.push_back( D3DXVECTOR3( 0, -1, 0 ) );

	for(size_t i = 0; i<24; ++i)
		texCoords.push_back( D3DXVECTOR2( 0, 0 ) );

	indices.push_back(0  );
	indices.push_back(1  );
	indices.push_back(2	 );
	indices.push_back(0	 );
	indices.push_back(2	 );
	indices.push_back(3	 );

	indices.push_back(4	 );
	indices.push_back(5	 );
	indices.push_back(6	 );
	indices.push_back(4	 );
	indices.push_back(6	 );
	indices.push_back(7	 );

	indices.push_back(8	 );
	indices.push_back(9	 );
	indices.push_back(10 );
	indices.push_back(8	 );
	indices.push_back(10 );
	indices.push_back(11 );

	indices.push_back(12 );
	indices.push_back(13 );
	indices.push_back(14 );
	indices.push_back(12 );
	indices.push_back(14 );
	indices.push_back(15 );

	indices.push_back(16 );
	indices.push_back(17 );
	indices.push_back(18 );
	indices.push_back(16 );
	indices.push_back(18 );
	indices.push_back(19 );

	indices.push_back(20 );
	indices.push_back(21 );
	indices.push_back(22 );
	indices.push_back(20 );
	indices.push_back(22 );
	indices.push_back(23 );

	// vertex data
	device->CreateVertexBuffer( sizeof(Vertex) * 24, 0, 0, D3DPOOL_MANAGED, &vBuffer, 0);
	Vertex *vbData = 0;
	vBuffer->Lock(0,0,(void**)&vbData,0);

	for( size_t i = 0; i < 24; ++i )
	{
		vbData[i].pos = positions[i];
		vbData[i].norm = normals[i];
		vbData[i].uv = texCoords[i];
	}
	vBuffer->Unlock();

	//triangles data
	device->CreateIndexBuffer( sizeof(DWORD) * indices.size(), D3DUSAGE_WRITEONLY, D3DFMT_INDEX32, D3DPOOL_DEFAULT, &iBuffer,0);
	DWORD *ibData = 0;
	iBuffer->Lock(0,0,(void**)&ibData,0);
	memcpy( (void*)ibData, (void*)indices.data(), sizeof(DWORD) * indices.size() );
	iBuffer->Unlock();

	obb.GenerateBoundingBox( positions );

	//create vertex declaration
	D3DVERTEXELEMENT9 elements[] = 
	{
		{ 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 },
		{ 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 },
		{ 0, 24, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},
		D3DDECL_END()
	};
	device->CreateVertexDeclaration(elements, &vdecl);
	
}
SimpleMeshLoader::~SimpleMeshLoader()
{
}

IDirect3DVertexBuffer9 *SimpleMeshLoader::GetVertices()
{
	return vBuffer;
}
IDirect3DIndexBuffer9 *SimpleMeshLoader::GetIndices()
{
	return iBuffer;
}

IDirect3DVertexDeclaration9 *SimpleMeshLoader::GetVertexDeclaration()
{
	return vdecl;
}

UINT SimpleMeshLoader::GetStride()
{
	return sizeof(Vertex);
}

OBB SimpleMeshLoader::GetOBB()
{
	return obb;
}

UINT SimpleMeshLoader::GetVertexCount()
{
	return 24;
}
UINT SimpleMeshLoader::GetTriangleCount()
{
	return 12;
}